package com.atguigu.flinksql;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

import java.time.ZoneOffset;

public class Flink10_Time_Event_SQL {
    public static void main(String[] args) {
        Configuration configuration = new Configuration();
        configuration.setInteger("rest.port",10000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(configuration);
        env.setParallelism(2);
        //由于有时间的差值，东八区的一个时间差
        //设置一下
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        tEnv.getConfig().setLocalTimeZone(ZoneOffset.ofHours(0));

        //创建表
        tEnv.executeSql("create table sensor(" +
                "   id string, " +
                "   ts bigint, " +
                "   vc int, " +
                "   et as TO_TIMESTAMP_LTZ(ts, 3), " +
                "   watermark for et as et - interval '3' second " +
                ")with(" +
                "   'connector'='filesystem', " +
                "   'path'='input/sensor.txt', " +
                "   'format'='csv' " +
                ")");

        //查询表的数据结构
        tEnv.from("sensor").printSchema();

        //输出
        tEnv.sqlQuery(" select * from sensor ").execute().print();
    }
}
